package com.lzx.iteam.net;

import android.content.Context;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import com.lzx.iteam.bean.ChatContact;
import com.lzx.iteam.bean.PhoneNumItem;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.PreferenceUtil;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

/**
 * Created by shijunting on 15-1-29.
 */
public class GroupContactMsg extends CcMsgStructure {

    private static final String TAG = "GetStructureMsg";

    private Message mmCallback;
    private Context mContext;

    public GroupContactMsg(Message callback,Context context) {
        mmCallback = callback;
        mContext=context;
    }

    @Override
    public void onReceive(JSONObject data) {
        try {
//            ArrayList<GroupContact> groupContacts = new ArrayList<GroupContact>();
            ArrayList<ChatContact> chatContacts = new ArrayList<ChatContact>();
            JSONObject jsonObject = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);
            JSONArray jsonArray = jsonObject.getJSONArray("contacts");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject item = jsonArray.getJSONObject(i);
//                GroupContact contactItem = new GroupContact();
//                contactItem.contact_id = item.getInt(AsynHttpClient.KEY_CONTACT_ID);
//                contactItem.has_tag = item.getBoolean(AsynHttpClient.KEY_HAS_TAG);
//                contactItem.contact_name = item.getString(AsynHttpClient.KEY_CONTACT_NAME);
//                contactItem.group_id = item.getInt(AsynHttpClient.KEY_STRUCTURE_GROUP_ID);
//                String number = "";
                if (item.has(AsynHttpClient.KEY_HAS_TAG)&&item.getBoolean(AsynHttpClient.KEY_HAS_TAG)) {
//                    String jNumListStr = item.getString(AsynHttpClient.KEY_CONTACT_PHONENUMBER);
//                    if (!TextUtils.isEmpty(jNumListStr)) {
//                        JSONArray jNumListArr = new JSONArray(jNumListStr);
//                        PhoneNumItem phoneItem = new PhoneNumItem();
//                        phoneItem.label = jNumListArr.getString(0);
//                        phoneItem.phoneNum = jNumListArr.getString(1);
//                        number = phoneItem.phoneNum;
//                    }
                    long cloudId =0;
                    try {
                        HashMap<Long, HashSet<Long>> contactId2CloudId = (HashMap<Long, HashSet<Long>>) PreferenceUtil.getInstance(mContext).getCloudContact(PreferenceUtil.CONTACT_ID_2_CLOUD_ID);
                        HashSet<Long> cloudIdSet = contactId2CloudId.get(item.getString(AsynHttpClient.KEY_CONTACT_ID));
                        cloudId = cloudIdSet.iterator().next();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    ChatContact chatContact = new ChatContact(cloudId+"", item.getString(AsynHttpClient.KEY_CONTACT_NAME),item.getString(AsynHttpClient.KEY_CONTACT_ID),item.getString("contact_image"));
                    chatContacts.add(chatContact);
                }
//                groupContacts.add(contactItem);
            }

            if (mmCallback != null) {
                mmCallback.obj = chatContacts;
                mmCallback.arg1 = 0;
                mmCallback.sendToTarget();
                return;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onError(int error, Object ret) {
        Log.d(TAG, "onError : " + error + " ret: " + ret);
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if(mmCallback != null){
            mmCallback.arg1 = error;
            if(!StringUtil.isEmpty(errorMsg)){
                mmCallback.obj = errorMsg;
            }else{
                mmCallback.obj = Constants.NETWORK_WRROR;
            }
            mmCallback.sendToTarget();
        }
    }
}
